package com.unei.guava.demo.fp;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunlei on 2015/1/21.
 */
public class PredicateDemo {
    @Test
    public void test(){
        TemperateClimatePredicate temperateClimatePredicate=new TemperateClimatePredicate();
        LowRainfallPredicate lowRainfallPredicate=new LowRainfallPredicate();

        PopulationPredicate smallPopulationPredicate=new PopulationPredicate();
        //Predicates.and
        Predicate smallAndDry= Predicates.and(smallPopulationPredicate,lowRainfallPredicate);

        //Predicates.or
        Predicate smallTemperate=Predicates.or(smallPopulationPredicate,temperateClimatePredicate);

        //Predicates.not
        Predicate largeCityPredicate=Predicates.not(smallPopulationPredicate);

        //Predicates.compose
        //Predicate<A> compose(Predicate<B> predicate, Function<A, ? extends B> function)
        //底层调用：predicate.apply(function.apply(A))
        Map<String,State> stateMap=new HashMap<String,State>();
        State s=new State("New York");
        s.setRegion("MIDEWEST");
        s.addCity("new");
        s.addCity("york");
        stateMap.put("NY",s);
        Function<String,State> lookup= Functions.forMap(stateMap);
        SouthwestOrMidwestRegionPredicate southwestOrMidwestRegionPredicate=new SouthwestOrMidwestRegionPredicate();
        Predicate<String> predicate=Predicates.compose(southwestOrMidwestRegionPredicate,lookup);
        System.out.println(predicate.apply("NY"));
    }
}
